package com.kuaimacode.kframework.admin.services.content;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.mybatis.mapper.index.HelpMapper;
import com.kuaimacode.kframework.mybatis.models.index.Help;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class HelpService extends ServiceImpl<HelpMapper, Help> {


    public List<Help> queryHelpList(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status","0");

        return baseMapper.selectList(queryWrapper);

    }

    /**
     *  分页查找帮助
     * @param pageNum 当前页码，从1开始
     * @param pageSize  一页显示多少行
     * @return
     */
    public PageInfo<Help> findAll(int pageNum, int pageSize,Help help) {
        PageHelper.startPage(pageNum, pageSize);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status","0");
        Optional.ofNullable(help.getTitle()).ifPresent(str->queryWrapper.like("title",str));

        List<Help> helps = baseMapper.selectList(queryWrapper);
        return new PageInfo<Help>(helps);
    }



    /**
     * 新建保存用户，自动生成salt盐，加密password保存
     * @param help
     * @return
     */
    public boolean createHelp(Help help) {
        if (null != help) {

            help.setId(null);
            help.setStatus("0");
            help.setCreateAt(new Date());
            return baseMapper.insert(help) > 0;
        }
        return false;
    }

}
